<?

/********************************************
* event.php
*
* This page displays the relevent information about an event.
*
********************************************/

//include files
include "config.php";
include "../lib/db.php";
include "../lib/tbatv_lib.php";

$tba = new tbatvController();

//get passed arguments
$eventid = sanatizeGet($_GET['eventid']);
if ($eventid == "") {
	$eventid = sanatizeGet($_GET['event']);
} 
if ($eventid == "") {
    $eventid = 0;
}
//look up what event we're dealing with
$eventid = stripslashes($eventid);
$event = new Event($eventid);

$mode = $_GET["mode"];
if (is_null($mode)) {
	if ($event->hasMatches()) {
		$mode = "match_results";
	} else {
		$mode = "teams";
	}
}

//include the header, which uses the matchinfo.
$title = "{$event->getName()} ({$event->getYear()})";

include "header.php";

//Echo the event information
?>
<h1><? echo $event->linkto() ?></h1>

<table class="table_alt">
	<tr class="table_toprow"><td colspan="2">Event Information: <? echo $event->getName()?> (<? echo $event->getYear() ?>)</td></tr>
	<tr class="table_row0"><td><strong>Week:</strong></td><td><? echo $tba->stringWeek($event->getWeek()) ?></td></tr>
	<tr class="table_row1"><td><strong>Event Location:</strong></td><td><? echo $event->getLocation()?></td></tr>
	<tr class="table_row0"><td><strong>Teams Attending:</strong></td><td><? echo $event->getTeamCount() ?></td></tr>
</table>
<p><a href="?eventid=<?echo $event->getEventID() ?>&mode=teams">Click here to view teams attending</a></p>
<p><a href="?eventid=<?echo $event->getEventID() ?>&mode=match_results">Click here for match listings</a></p>

<? 

if ($mode == "teams") {
	
	?><h2>Teams Attending the <?echo $event->getName() ?></h2>
	<p>Below is a list of teams attending the <?echo $event->getYear() . " " . $event->getName() ?>. This list may not be final.</p>	<?
	
	$list_of_teams = $event->getAllTeams();
		
	if (count($list_of_teams) == 0) {
		?><h2>No Teams Attending</h2>
		<p>Currently, no teams are known to be registered for the <strong><?echo $event->getYear() . " " . $event->getName() ?></strong>. Please check back later!</p><?
	} else {
		foreach ($list_of_teams as $team) {
			if ($team->getInformalName() != "") {
				$linktext = "Team " . $team->getTeamNumber() . " - " . $team->getInformalName();
			} else {
				$linktext = "Team " . $team->getTeamNumber();
			}
			echo $team->linkto($event->getYear(), $linktext);
			echo "<br />\r\n";
		}
	}
}

if ($mode == "match_results") {
	//iterate through levels of play and output matches for each one. This looping iterative method is a huge improvement
	//over the old "grab all at once" method.
	
	?>
	<h2>Match Listing</h2>
	<? 
	$complevels = array(10, 20, 30, 40, 50);
	foreach ($complevels as $complevel) {
		$list_of_matches = $event->getAllMatches($complevel);
		if (count($list_of_matches) > 0) {
			echo "<h3>" . $tba->stringComplevel($complevel) . "</h3>\r\n";
			foreach ($list_of_matches as $match) {
				include "views/mobile_matchrow.php";
			}
		}
	}
}

include "footer.php";
?>